//
//  DWDStatusModel.swift
//  dwdWb
//
//  Created by Dengdwd on 15/10/12.
//  Copyright © 2015年 dwd. All rights reserved.
//

import UIKit

class DWDStatusModel: NSObject {
    var rowHeight:CGFloat = 0
    /// 微博对象
    var status: DWDStatuses
    /// 用户头像 URL
    var userIconUrl: NSURL? {
        return NSURL(string: status.user?.profile_image_url ?? "")
    }
    var cellID:String? {
        return (status.retweeted_status != nil) ? DWDFOWARDCELL : DWDSTATUSCELL
    }
    
    var userVipImage: UIImage? {
        switch (status.user?.verified ?? -1) {
        case 0: return UIImage(named: "avatar_vip")
        case 2, 3, 5: return UIImage(named: "avatar_enterprise_vip")
        case 220: return UIImage(named: "avatar_grassroot")
        default: return nil
        }
    }
//    来源文本
    var sourceText:String? {
    
        let source = status.source! as NSString
        let preRange = source.rangeOfString("\">")
        if preRange.location != NSNotFound {
            let sufRange = source.rangeOfString("</")
            if sufRange.location != NSNotFound {
            let preLocation = preRange.location + preRange.length
            let sufLocation = sufRange.location
             return source.substringWithRange(NSMakeRange(preLocation, sufLocation - preLocation))
            }     
        }
        return "新浪微博"
    }
    var creatDateStr:String?{
        var result = ""
        let formatter = NSDateFormatter()
        formatter.dateFormat = "EEE MMM dd HH:mm:ss z yyyy"
        formatter.locale = NSLocale(localeIdentifier: "en_US")
        let creatDate = formatter.dateFromString(status.created_at!)
        let currentDate = NSDate()
        if currentDate.isThisYear(creatDate!) {
            if currentDate.isToday(creatDate!) {
                let timeInterval = currentDate.timeIntervalSinceDate(creatDate!)
                if timeInterval<60 {
                    result = "刚刚"
                }else if timeInterval < 60 * 60 {
                    result = "\(Int(timeInterval) / 60)分钟前"
                }else{
                    result = "\(Int(timeInterval) / (60 * 60))小时前"
                }
            }else if currentDate.isYesterday(creatDate!){
            formatter.dateFormat = "昨天 HH:mm"
            result = formatter.stringFromDate(currentDate)
            }else{
            formatter.dateFormat = "MM-dd HH:mm"
            result = formatter.stringFromDate(currentDate)
            result = " \(result)"
            }
        } else {
        formatter.dateFormat = "yyyy-MM-dd"
        result = formatter.stringFromDate(currentDate)
        }
        return result
    }
    
    /// 会员等级 1-6
    var userMemberImage: UIImage? {
        if status.user?.mbrank > 0 && status.user?.mbrank < 7 {
            return UIImage(named: "common_icon_membership_level\(status.user!.mbrank)")
        }
        return nil
    }
    var forwardText: String? {
        let username = status.retweeted_status?.user?.name ?? ""
        let text = status.retweeted_status?.text ?? ""
        
        return "@\(username):\(text)"
    }
    // MARK: - 构造函数
    //        定义一个图片NSURL数组,将模型里的字典数组转换成URL数组
    var thumbnailUrls:[NSURL]?
    init(status: DWDStatuses) {
        self.status = status
        if let urls = status.retweeted_status?.pic_urls ?? status.pic_urls {
            thumbnailUrls = [NSURL]()
            for picDict in urls {
                thumbnailUrls?.append(NSURL(string: picDict["thumbnail_pic"]!)!)
            }
        }
        super.init()
    }
    //    打印模型属性小技巧
    override var description: String {
        return status.description + " 缩略图 URL 数组 \(thumbnailUrls)"
    }

}
